'use strict';
import Base from './base.js';
//美易通订单类控制器请求方法
module.exports = class extends Base {
//获取列表通用的接口
async getorderlistallAction(){
    let orcode = this.get('orcode');
    let jsonshop = this.get('jsonshop');
    let floor = this.get('floor');
    let ismanage = this.get('ismanage');
    let user_id = this.get('user_id');
    let select_info = this.get('select_info');
    let page = this.get('page');
    let size = this.get('size');
    let nowpage = page * size;

    switch(floor)
    {
      case '0':
      case '1':
       var info = await this.model().query(' SELECT og.name as username,od.name as shopname,og.sex,oso.addtime,og.headurl,oso.status,oso.fenpei,oso.recemoney,oso.cardreduce,oso.usebalance,usegiftbalance,og.id as guestid,oso.id as orderid FROM `think_or_shop_order` oso LEFT JOIN `think_or_guest` og ON oso.guestid = og.id LEFT JOIN `think_or_department` od ON od.id = oso.departmentid WHERE od.id IN ('+jsonshop+') '+select_info+' ORDER BY oso.addtime DESC LIMIT '+nowpage+','+size+' ');
      break;
      case '2':
       var info = await this.model().query( ' SELECT og.name as username,od.name as shopname,og.sex,oso.addtime,og.headurl,oso.status,oso.fenpei,oso.recemoney,oso.cardreduce,oso.usebalance,usegiftbalance,og.id as guestid,oso.id as orderid FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_shop_order` oso ON osy.orderid = oso.id LEFT JOIN `think_or_department` od ON oso.departmentid = od.id  WHERE au.bumenid = '+jsonshop+' '+select_info+' GROUP BY osy.orderid ORDER BY oso.addtime DESC LIMIT '+nowpage+','+size+' ' );
      break;
      case '3':
        if (ismanage == 1) {
          info = await this.model().query( ' SELECT og.name as username,od.name as shopname,og.sex,oso.addtime,og.headurl,oso.status,oso.fenpei,oso.recemoney,oso.cardreduce,oso.usebalance,usegiftbalance,og.id as guestid,oso.id as orderid FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_shop_order` oso ON osy.orderid = oso.id LEFT JOIN `think_or_department` od ON oso.departmentid = od.id  WHERE au.xiaozuid  = '+jsonshop+' '+select_info+' GROUP BY osy.orderid ORDER BY oso.addtime DESC LIMIT '+nowpage+','+size+' ' );
        } else {
          info = await this.model().query( ' SELECT og.name as username,od.name as shopname,og.sex,oso.addtime,og.headurl,oso.status,oso.fenpei,oso.recemoney,oso.cardreduce,oso.usebalance,usegiftbalance,og.id as guestid,oso.id as orderid FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_shop_order` oso ON osy.orderid = oso.id LEFT JOIN `think_or_department` od ON oso.departmentid = od.id  WHERE osy.userid  = '+user_id+' '+select_info+' GROUP BY osy.orderid ORDER BY oso.addtime DESC LIMIT '+nowpage+','+size+' ');
        }
      break;
    }

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info});   
}
//根据orderid 查询order的详细信息
async getorderinfoAction(){
    let orderid = this.post('orderid');
    let order = await this.model().query(' SELECT og.name,oso.order_sn,oso.addtime,oso.recemoney,oso.cardreduce,oso.usebalance,oso.usegiftbalance,od.name as shopname,au.name as gendanname,op.name as jobname FROM `think_or_shop_order` oso LEFT JOIN `think_or_guest` og ON oso.guestid = og.id LEFT JOIN `think_or_department` od ON oso.departmentid = od.id LEFT JOIN `think_or_app_user` au ON oso.gendanid = au.id LEFT JOIN `think_or_position` op ON au.positionid = op.id WHERE oso.id = '+orderid+' ');
    let order_goods = await this.model().query(' SELECT type,typename,num,price,amount,consumetype FROM `think_or_shop_orderinfo` WHERE orderid = '+orderid+' ');
    let fuwu = await this.model().query('SELECT  osy.id,au.name,op.name as jobname FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_position` op ON au.positionid = op.id WHERE osy.orderid = '+orderid+' ');
    
    return think.isEmpty(order)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'order':order,'order_goods':order_goods,'fuwu':fuwu});
  }
//获取客户订单列表
async getorderguestlistAction(){
  let guest_id = this.get('guest_id');
  let page = this.get('page');
  let size = this.get('size');
  let nowpage = page * size;
  let info = await this.model().query(' SELECT  og.name as username,od.name as shopname,og.sex,oso.addtime,og.headurl,oso.status,oso.fenpei,oso.recemoney,oso.cardreduce,oso.usebalance,usegiftbalance,og.id as guestid,oso.id as orderid FROM `think_or_shop_order` oso LEFT JOIN `think_or_guest` og ON oso.guestid = og.id LEFT JOIN `think_or_department` od ON oso.departmentid = od.id WHERE oso.guestid = ' + guest_id + ' ORDER BY oso.addtime DESC LIMIT '+nowpage+','+size+'  ');
  return think.isEmpty(info) ? this.json({ 'code': "200", 'msg': "没有数据...", 'data': '' }) : this.json({ 'code': "100", 'msg': "查找成功...", 'data': info });
 }

//bottom
};